package com.ceair.authorization.wechat;

import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest;
import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequestEntityConverter;
import org.springframework.util.MultiValueMap;

import static com.ceair.constant.SecurityConstants.*;

/**
 * @author wangbaohai
 * @ClassName WechatCodeGrantRequestEntityConverter
 * @description: 微信登录请求token入参处理类
 * @date 2024年11月27日
 * @version: 1.0.0
 */
public class WechatCodeGrantRequestEntityConverter extends OAuth2AuthorizationCodeGrantRequestEntityConverter {

    /**
     * 重写创建参数的方法，以便为特定的登录方式（如微信）添加额外的参数
     *
     * @param authorizationCodeGrantRequest 授权码请求对象，包含客户端注册信息等
     * @return 返回一个包含参数的多值映射
     */
    @Override
    protected MultiValueMap<String, String> createParameters(OAuth2AuthorizationCodeGrantRequest authorizationCodeGrantRequest) {
        // 调用父类方法创建参数
        MultiValueMap<String, String> parameters = super.createParameters(authorizationCodeGrantRequest);
        // 获取客户端注册ID，用于判断登录方式
        String registrationId = authorizationCodeGrantRequest.getClientRegistration().getRegistrationId();
        // 判断是否为微信登录
        if (THIRD_LOGIN_WECHAT.equals(registrationId)) {
            // 如果当前是微信登录，携带appid和secret
            parameters.add(WECHAT_PARAMETER_APPID, authorizationCodeGrantRequest.getClientRegistration().getClientId());
            parameters.add(WECHAT_PARAMETER_SECRET,
                    authorizationCodeGrantRequest.getClientRegistration().getClientSecret());
        }
        // 返回参数映射
        return parameters;
    }

}
